package net.digitalartwork.ui{
	import flash.display.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.*;
	import flash.events.*;
	import flash.text.*;
	
	import gs.TweenFilterLite;
	import gs.OverwriteManager;
	import gs.easing.*;
	
	
	public class BasicButton extends MovieClip{
		public var bg:MovieClip;
		public var btn_text_mc:MovieClip;

		public var MOUSECLICK:String = "mousedown";
		public var TOGGLE1:String = "toggle1";
		public var TOGGLE2:String = "toggle2";
		public var SIZECHANGE:String = "sizechange";
		
		private var toggleText1:String = "toggleText1";
		private var toggleText2:String = "toggleText2 is longer";
		
		public function BasicButton() {
			trace("BasicButton Init");
			//OverwriteManager.init();
			//--------------------------------------------------------------------------
			// Button Text
			//--------------------------------------------------------------------------
			btn_text_mc.mouseEnabled = false;
			btn_text_mc.mouseChildren = false;
			btn_text_mc.btn_text.autoSize = "left";
			btn_text_mc.btn_text.text = toggleText1;
			btn_text_mc.alpha = .8;
			//TweenFilterLite.to(btn_text_mc, .2, {alpha:.1});
			//--------------------------------------------------------------------------
			// Button BG
			//--------------------------------------------------------------------------
			bg.width = btn_text_mc.width + 20;
			bg.btn_bg_bounds.alpha = .25;
			bg.btn_toggle2.visible = false;
			bg.btn_toggle1.visible = true;
			
			bg.buttonMode = true; 
			bg.useHandCursor = true;
			bg.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler); 
			bg.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler); 
			bg.addEventListener(MouseEvent.CLICK, onClickHandler); 
			//bg.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler); 
			//bg.addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler);
		}
		
		private function onRollOverHandler(e:MouseEvent):void {
			btn_text_mc.alpha = 1;
			bg.btn_bg_bounds.alpha = .5;
		}
		private function onRollOutHandler(e:MouseEvent):void {
			btn_text_mc.alpha = .8;
			bg.btn_bg_bounds.alpha = .25;
		}
		
		public function setToggleActive(nu:Number=0):void {
			if (nu == 0) {
				bg.btn_toggle2.visible  = false;
				bg.btn_toggle1.visible = true;
				toggleState(false);
				trace("TOGGLE1");
			} else {
				bg.btn_toggle2.visible  = true;
				bg.btn_toggle1.visible = false;
				toggleState(true);
			}
		}
		
		private function onClickHandler(e:MouseEvent):void {
			dispatchEvent(new Event(MOUSECLICK));
			if (bg.btn_toggle2.visible == true) {
				bg.btn_toggle2.visible  = false;
				bg.btn_toggle1.visible = true;
				toggleState(false);
				trace("TOGGLE1");
				dispatchEvent(new Event(TOGGLE1));
			} else {
				bg.btn_toggle2.visible  = true;
				bg.btn_toggle1.visible = false;
				toggleState(true);
				dispatchEvent(new Event(TOGGLE2));
			}
		}
		private function toggleState(s:Boolean = false):void {
			TweenFilterLite.to(btn_text_mc, .1, {alpha:0, onComplete:changeText});
			function changeText():void {
				if (s) {
					btn_text_mc.btn_text.text = toggleText2;
				} else {
					btn_text_mc.btn_text.text = toggleText1;
				}
				TweenFilterLite.to(bg, .2, {width:(btn_text_mc.width + 20)});
				
				TweenFilterLite.to(btn_text_mc, .1, {delay:.2, alpha:1});
				//bg.width = btn_text_mc.width + 10;
				dispatchEvent(new Event(SIZECHANGE));
			}
			
		}
		
		public function setButtonText(t1:String, t2:String):void {
			toggleText1 = t1;
			toggleText2 = t2;
			toggleState();
		}
		
	}
	
}